package com.open.party.cloud.viewModel

import cn.android.x.model.AppVersionEntity
import cn.android.x.model.LeaderUserBean
import cn.android.x.model.data.ResultInfo
import com.open.party.cloud.ServerConfig
import com.open.party.cloud.model.UserEntity
import com.open.party.cloud.model.UserVo
import com.open.party.cloud.model.data.Api
import com.open.party.cloud.model.data.LocalCache
import com.sinothk.rxretrofit.RxRetrofit
import org.greenrobot.eventbus.EventBus
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import java.util.*

class BaseViewModel {
    fun updateToken(account: String, pwd: String) { // 更新token
        RxRetrofit.init(ServerConfig.SERVER_URL) //, ServerConfig.getHeader()
            .create(Api::class.java)
            .updateToken(account, pwd)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<UserEntity>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<UserEntity>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = "updateToken"
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<UserEntity>) {
                    result.eventType = "updateToken"
                    EventBus.getDefault().post(result)
                }
            })
    }

    // 登录
    fun doLogin(account: String, password: String) {
//        val pwdMd5Str = XUtils.codeByMd5().stringToMD5(password);
        RxRetrofit.init(ServerConfig.SERVER_URL) //, ServerConfig.getHeader()
            .create(Api::class.java)
            .login(account, password)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<UserEntity>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    EventBus.getDefault().post(ResultInfo<UserEntity>(600, ResultInfo.getErrorMsg(e.message)))
                }

                override fun onNext(result: ResultInfo<UserEntity>) {
                    EventBus.getDefault().post(result)
                }
            })
    }

    // 人才工作 》专家列表
    fun getRenCaiLeaderListData(busType: String) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getRenCaiLeaderListData(busType)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<ArrayList<LeaderUserBean>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<ArrayList<LeaderUserBean>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = busType
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<ArrayList<LeaderUserBean>>) {
                    result.eventType = busType
                    EventBus.getDefault().post(result)
                }
            })
    }

    // 注册用户
    fun doRegister(userVo: UserVo) {
        RxRetrofit.init(ServerConfig.SERVER_URL) //, ServerConfig.getHeader()
            .create(Api::class.java)
            .doRegister(userVo)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<String>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<String>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = "doRegister"
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<String>) {
                    result.eventType = "doRegister"
                    EventBus.getDefault().post(result)
                }
            })
    }

    // 修改密码
    fun updatePassword(userVo: UserVo) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .updatePassword(userVo)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<String>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<String>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = "updatePassword"
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<String>) {
                    result.eventType = "updatePassword"
                    EventBus.getDefault().post(result)
                }
            })
    }

    // 版本检测
    fun getAppInfoForLastVersion(flag: String) {
        RxRetrofit.init(ServerConfig.SERVER_URL) //, ServerConfig.getHeader()
            .create(Api::class.java)
            .appInfoForLastVersion
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppVersionEntity>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val resultInfo = ResultInfo<AppVersionEntity>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message));
                    resultInfo.eventType = flag
                    EventBus.getDefault().post(resultInfo)
                }

                override fun onNext(resultInfo: ResultInfo<AppVersionEntity>) {
                    resultInfo.eventType = flag
                    EventBus.getDefault().post(resultInfo)
                }
            })
    }

    // 更新用户头像
    fun updateUserPhoto(avatarUrl: String) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .updateUserPhoto(avatarUrl)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<String>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val resultInfo = ResultInfo<String>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message));
                    resultInfo.eventType = "updateUserPhoto"
                    EventBus.getDefault().post(resultInfo)
                }

                override fun onNext(resultInfo: ResultInfo<String>) {
                    resultInfo.eventType = "updateUserPhoto"
                    EventBus.getDefault().post(resultInfo)
                }
            })
    }

    fun getPhoneCode(phoneNum: String, busType: String) {
        RxRetrofit.init(ServerConfig.SERVER_URL) //, ServerConfig.getHeader()
            .create(Api::class.java)
            .getPhoneCode(phoneNum, busType)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<String>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    EventBus.getDefault().post("访问服务器异常")
                }

                override fun onNext(result: ResultInfo<String>) {
                    if (result.code == ResultInfo.SUCCESS) {
                        EventBus.getDefault().post("")
                    } else {
                        EventBus.getDefault().post(result.msg)
                    }
                }
            })
    }

    fun resetPassword(phoneStr: String, phoneCodeStr: String, passWordStr: String) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .updatePasswordForForget(phoneStr, phoneCodeStr, passWordStr)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<String>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<String>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = "resetPassword"
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<String>) {
                    result.eventType = "resetPassword"
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun updateUserInfo(name: String) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .updateUserInfo(name)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<String>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<String>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = "updateUserInfo"
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<String>) {
                    result.eventType = "updateUserInfo"
                    EventBus.getDefault().post(result)
                }
            })
    }
}